package com.software.levelupandroid.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.software.levelupandroid.R;


/**
 * @Description TODO
 * @Author LiangYaJie
 * @CreateTime 2024年05月09日 10:15:17
 */
public class NumberCountView extends LinearLayout {

    private ImageView ivNumCountAdd, ivNumCountMinus;
    private EditText edtNumCountValue;
    private int currentValue; //当前数值
    private int max; //最大值
    private int min; //最小值
    private int step; //步长
    private int defaultValue; //默认值
    private OnValueChangeListener onValueChangeListener;

    public NumberCountView(Context context) {
        this(context, null, 0);
    }

    public NumberCountView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public NumberCountView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        getAttrs(context, attrs);
    }

    public int getCurrentValue() {
        currentValue = Integer.parseInt(String.valueOf(edtNumCountValue.getText()));
        return currentValue;
    }

    public void setCurrentValue(int currentValue) {
        this.currentValue = currentValue;
        updateText();
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }

    public int getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(int defaultValue) {
        currentValue = this.defaultValue = defaultValue;
        updateText();
    }

    private void getAttrs(Context context, AttributeSet attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NumberView);
        max = typedArray.getInt(R.styleable.NumberView_max, 999999);
        min = typedArray.getInt(R.styleable.NumberView_min, 0);
        step = typedArray.getInt(R.styleable.NumberView_step, 1);
        defaultValue = typedArray.getInt(R.styleable.NumberView_defaultValue, 1);
        currentValue = defaultValue; //当前值等于默认值
        if (currentValue == min){
            ivNumCountMinus.setEnabled(false);
        }else {
            ivNumCountMinus.setEnabled(true);
        }
    }

    private void initView(Context context){
        View view = LayoutInflater.from(context).inflate(R.layout.view_number_count, this, false);
        this.addView(view);
        ivNumCountAdd = view.findViewById(R.id.iv_numCount_add);
        ivNumCountMinus = view.findViewById(R.id.iv_numCount_minus);
        edtNumCountValue = view.findViewById(R.id.edt_numCount_value);

        ivNumCountAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //每次加减时，先获取当前数字框中的值，以防用户直接修改了数字框中的值，但是currentValue还是修改之前的值
                currentValue = Integer.parseInt(String.valueOf(edtNumCountValue.getText()));
                //先加完再比较，只要一点加号，减号就可以点击了
                currentValue += step;
                ivNumCountMinus.setEnabled(true);
                //为了防止超过最大值，最后一步将最大值设置成当前值
                if (currentValue >= max){
                    currentValue = max;
                    ivNumCountAdd.setEnabled(false);
                }
                //更新UI
                updateText();
                //回调当前值
                if (onValueChangeListener != null){
                    onValueChangeListener.onValueChange(currentValue);
                }
            }
        });

        ivNumCountMinus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                currentValue = Integer.parseInt(String.valueOf(edtNumCountValue.getText()));
                currentValue -= step;
                ivNumCountAdd.setEnabled(true);
                if (currentValue <= min){
                    currentValue = min;
                    ivNumCountMinus.setEnabled(false);
                }
                updateText();
                if (onValueChangeListener != null){
                    onValueChangeListener.onValueChange(currentValue);
                }
            }
        });
    }

    private void updateText() {
        edtNumCountValue.setText(currentValue + "");
    }

    public interface OnValueChangeListener{
        void onValueChange(int value);
    }

    public void setOnValueChangeListener(OnValueChangeListener onValueChangeListener){
        this.onValueChangeListener = onValueChangeListener;
    }

}
